home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / upstat < prev    next >
Encoding:
AWK Script  |  1997-08-26  |  4.0 KB  |  137 lines

  1. #!/usr/local/bin/gawk -f
  2. #!/usr/bin/awk -f
  3. # upstat: process wtmp boot time entries & generate statistics
  4. # @(#) upstat.gawk 2.1 96/06/18
  5. # 89/07 john h. dubois iii (john@armory.com) (DOS version)
  6. # 90/11/23 Ported to XENIX: assorted changes; mainly changed expected date
  7. #          format to that produced by who -a:
  8. #             .       system boot  Oct  5 15:51
  9. # 91/10/28 changed name to upstat
  10. # 92/01/06 added help option
  11. # 92/02/08 added ability to handle year spanning wtmp files
  12. # 96/06/18 Print correct line numbers in error messages.
  13.  
  14. BEGIN {
  15.     if (ARGV[1] ~ "^[-+]h$") {
  16.     print \
  17. "upstat: generate uptime statistics from wtmp boot time entries.\n" \
  18. "Usage: upstat [-h] [who-output]\n" \
  19. "[who-output] should be the output of who -a.\n" \
  20. "If it is not given, upstat uses the output of who -a /etc/wtmp."
  21.     exit(0)
  22.     }
  23.     MonthList = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
  24.     # This does not work if wtmp spans a year-end.
  25.     # So, assume every year is a leap year.
  26.     # At worst, an extra day of no boots (Feb. 29) is added.
  27.     #"/bin/date +%y" | getline Y
  28.     #if (Y % 4)
  29.     #    DaysList = "0 31 59 90 120 151 181 212 243 273 304 334 365"
  30.     #else
  31.     DaysList = "0 31 60 91 121 152 182 213 244 274 305 335 366"
  32.     split(MonthList,Months,",")
  33.     split(DaysList,NumDays," ")
  34.     for (Month in Months)
  35.     Days[Months[Month]] = NumDays[Month]
  36.     MinDiffTime = 2000000000
  37.     Year = 0
  38.     MinuteSecs = 60
  39.     HourSecs = MinuteSecs * 60
  40.     DaySecs = HourSecs * 24
  41.     YearSecs = DaySecs * 366
  42.  
  43.     if (ARGC > 1)
  44.     while ((getline < ARGV[1]) == 1)
  45.         ProcLine()
  46.     else
  47.     while (("exec who -a /etc/wtmp" | getline) == 1)
  48.         ProcLine()
  49.  
  50.     if (DisconCt || BadYearCt)
  51.     printf("Errors: %d time discontinuities, %d bad years.\n",
  52.     DisconCt,BadYearCt);
  53.     LogSecs = TotSec - FirstSec
  54.     printf("Period covered by log: %s (%s to %s)\n",
  55.     SecToTime(LogSecs),FirstDate,LastDateTime)
  56.     printf "Year-ends spanned by log: %d.\n",Year
  57.     printf("Number of reboots: %d.\n",BootLines)
  58.     printf("Number of days on which machine was rebooted: %d.\n",NumRebootDays);
  59.     printf("Fraction of days on which machine was rebooted: 1/%.1f.\n",
  60.      (1.0 * LogSecs / 86400.0)/NumRebootDays);
  61.     printf("Shortest period between reboots: %s (%s to %s)\n",
  62.     SecToTime(MinDiffTime),MinLastDate,MinDate)
  63.     printf("Longest period between reboots: %s (%s to %s)\n",
  64.     SecToTime(MaxDiffTime),MaxLastDate,MaxDate)
  65.     printf("Average period between reboots: %s.\n",
  66.      SecToTime(int(LogSecs/BootLines)));
  67. }
  68.  
  69. function ProcLine() {
  70.     LineNum++
  71.     if (($2 != "system") || ($3 != "boot"))
  72.     return
  73.     Date = $4 " " $5
  74.     DateTime = Date " " $6
  75.     if (($4 == "Jan") && (LastDate ~ "^Dec")) {
  76.     Year++
  77.     printf "Year change on line %d: %s to %s.\n",LineNum,LastDate,Date
  78.     }
  79.     TotSec = EntryToSec(Year,$4,$5,$6)
  80.     DiffTime = TotSec - LastTotSec
  81.     LastTotSec = TotSec
  82.     if (DiffTime < 0) {
  83.     printf("Error on line %d: time discontinuity.\n",LineNum)
  84.     DisconCt++
  85.     Discon = 1
  86.     return
  87.     }
  88.     if (Date != LastDate)
  89.     NumRebootDays++;
  90.     BootLines++
  91.     if (FirstDate == "") {
  92.     FirstDate = DateTime
  93.     FirstSec = TotSec
  94.     }
  95.     else if (Discon) 
  96.     Discon = 0
  97.     else {
  98.     if (DiffTime < MinDiffTime) {
  99.         MinDiffTime = DiffTime
  100.         MinDate = DateTime
  101.         MinLastDate = LastDateTime
  102.     }
  103.     if (DiffTime > MaxDiffTime) {
  104.         MaxDiffTime = DiffTime;
  105.         MaxDate = DateTime
  106.         MaxLastDate = LastDateTime
  107.     }
  108.     }
  109.     LastDateTime = DateTime
  110.     LastDate = Date
  111. }
  112.  
  113. function SecToTime(Seconds,  Days,Hours,Minutes,Time) {
  114.     Days = int(Seconds / 86400)
  115.     Seconds %= 86400
  116.     Hours = int(Seconds / 3600)
  117.     Seconds %= 3600
  118.     Minutes = int(Seconds / 60)
  119.     Seconds %= 60
  120.     if (Days)
  121.     Time = Days "d "
  122.     if (Time || Hours)
  123.     Time = Time Hours "h "
  124.     if (Time || Minutes)
  125.     Time = Time Minutes "m "
  126.     if (!Time || Seconds)
  127.     Time = Time Seconds "s "
  128.     return substr(Time,1,length(Time) - 1)
  129. }
  130.  
  131. function EntryToSec(Year,Month,Day,HM)
  132. {
  133.     split(HM,HMArr,":")
  134.     return HMArr[2] * MinuteSecs + HMArr[1] * HourSecs + \
  135.     (Days[Month] + Day - 1) * DaySecs + Year * YearSecs
  136. }
  137.